package com.hao.shiro.realm;

import com.hao.shiro.service.SecurityService;
import com.hao.shiro.service.impl.SecurityServiceImpl;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * TODO
 * 一个自定义的Realm
 * @author 97472
 * @version 1.0
 * @date 2021/8/28 15:52
 */
public class DefinitionRealm extends AuthorizingRealm {

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        return null;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        SecurityService securityService = new SecurityServiceImpl();
        String password = securityService.findPasswordByLoginName(username);
        if ("".equals(password) || password == null){
            throw new UnknownAccountException();
        }

        return new SimpleAuthenticationInfo(username,password,getName());
    }
}
